package top.ivansong92.example.leetcode.learning.other;

import org.junit.Test;

import java.util.*;

/**
 * 回溯案例
 */
public class RecallExamp {


    @Test
    public void testGenerateParenthesis() {
        System.out.println(String.join("|", generateParenthesis(3)));
    }

    /**
     * 22. 括号生成
     * @param n
     * @return
     */
    public List<String> generateParenthesis(int n) {
        if (n <= 0) {
            return Collections.emptyList();
        }
        if (n == 1) {
            return Collections.singletonList("()");
        }
        List<String> res = new ArrayList<>();
        doGenerateParenthesis(res, "", 0, 0, n);
        return res;
    }

    private void doGenerateParenthesis(List<String> list, String string, int left, int right, int n) {
        if (left == n && right == n) {
            list.add(string);
            return;
        }

        if (left > n || right > n || left < right) {
            return;
        }

        doGenerateParenthesis(list, string + "(", left + 1, right, n);
        doGenerateParenthesis(list, string + ")", left, right + 1, n);
    }


    /**
     * 39
     */

    /**
     * 40
     */
    /**
     * 216
     */
    /**
     * 78
     */
    /**
     * 90
     */
    /**
     * 401
     */
}
